import axios from "axios";
import store from "../store/index"; //引入状态管理 store
import Common from "../common/index";
import Utility from "../common/Utility";

let baseURL = process.env.srconfig;

store.state.userInfo.env = baseURL;
// console.log("token", Utility.getSearchString("token"));

export function request(config) {
  const instance = axios.create({
    baseURL: baseURL,
    timeout: 8000,
    headers: {
      "content-type": "application/json",
      "verify-type": "type",
      "login-token": Utility.getSearchString("token")
        ? store.state.userInfo.fuid + "-" + Utility.getSearchString("token")
        : "ZEGOPAY-REGISTER-USER"

      // store.state.userInfo.fuid + "-" +
      //    "ZEGOPAY-REGISTER-USER"
      // store.state.userInfo.fuid + "-" + Utility.getSearchString("token")
    },
    method: "post"
  });
  // 请求前拦截
  instance.interceptors.request.use(
    config => {
      Common.loading();
      return config;
    },
    err => {
      Common.hide();
      // //console.log("请求超时");
      return Promise.reject("TIME_OUT");
    }
  );

  // 返回后拦截
  instance.interceptors.response.use(
    data => {
      // alert(555)
      // //console.log(data)
      // alert(data.data.code)
      if (data.data.code == "200") {
        // Common.toast({message:err.data.message,time:2000})
        // alert(66)

        // Common.toast({
        //   message: Utility.getSearchString("token"),
        //   time:3000
        // })

        Common.hide();
        // Common.toast({
        //     message:lang.get("REQUEST_OK"),
        //     time:2000
        // })
        return data;
      }
      if (data.data.code == "400") {
        Common.hide();
        // //console.log(data)
        return Promise.reject(data.data.datas.error);
      }
      return Promise.reject(data.data.datas.error);
    },
    err => {
      // if (err.response.status === 504 || err.response.status === 404) {
      //     //console.log("服务器被吃了⊙﹏⊙∥");
      // } else if (err.response.status === 401) {
      //     //console.log("登录信息失效⊙﹏⊙∥");
      // } else if (err.response.status === 500) {
      //     //console.log("服务器开小差了⊙﹏⊙∥");
      // }
      // //console.log(err)
      Common.hide();
      // Common.toast({
      //     message:"Network Error",
      //     time:2000
      // })
      return Promise.reject("netWork");
    }
  );

  return instance(config);
}
